import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:libflutter/LifecycleWidget.dart';
import 'package:libflutter/PullToRefreshPage.dart';
import 'package:libflutter/TestAlignWidget.dart';
import 'package:libflutter/TestButtonWidget.dart';
import 'package:libflutter/TestClipWidget.dart';
import 'package:libflutter/TestContainerWidget.dart';
import 'package:libflutter/TestDecoratedBoxWidget.dart';
import 'package:libflutter/TestFittedBoxWidget.dart';
import 'package:libflutter/TestFlexWidget.dart';
import 'package:libflutter/TestFormWidget.dart';
import 'package:libflutter/TestIconWidget.dart';
import 'package:libflutter/TestImageWidget.dart';
import 'package:libflutter/TestLayoutWidget.dart';
import 'package:libflutter/TestListViewWidget.dart';
import 'package:libflutter/TestLocationWidget.dart';
import 'package:libflutter/TestNetDioWidget.dart';
import 'package:libflutter/TestPaddingWidget.dart';
import 'package:libflutter/TestPageViewWidget.dart';
import 'package:libflutter/TestProgressWidget.dart';
import 'package:libflutter/TestRefreshAndLoadMoreWidget.dart';
import 'package:libflutter/TestRiverpodWidget.dart';
import 'package:libflutter/TestRowWidget.dart';
import 'package:libflutter/TestRxDartWidget.dart';
import 'package:libflutter/TestSQLWidget.dart';
import 'package:libflutter/TestScaffoldWidget.dart';
import 'package:libflutter/TestSpannableWidget.dart';
import 'package:libflutter/TestStackWidget.dart';
import 'package:libflutter/TestSwitchAndCheckBoxWidget.dart';
import 'package:libflutter/TestTextFieldWidget.dart';
import 'package:libflutter/TestTextWidget.dart';
import 'package:libflutter/TestTransformWidget.dart';
import 'package:libflutter/TestWillPopScopeWidget.dart';
import 'package:libflutter/TestWrapWidget.dart';
import 'package:libflutter/db/SQLPageWidget.dart';
import 'package:libflutter/ext/ExtNavigator.dart';
import 'package:libflutter/logger/logger.dart';
import 'package:libflutter/main.dart';
import 'package:libflutter/net/RDApi.dart';
import 'package:libflutter/rd/LoginByPasswordWidget.dart';
import 'package:libflutter/rd/RDNetInheritedWidget.dart';
import 'package:libflutter/weather/TestWeacherAppWidget.dart';
import 'package:mobx/mobx.dart';

class DebugWidget extends StatelessWidget {
  const DebugWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Debug"),
      ),
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: Column(
          children: [
            ItemButton(
                value: "天气 开发 APP",
                callback: () {
                  open(context, const TestWeacherAppWidget());
                }),
            ItemButton(
                value: "WillPopScope",
                callback: () {
                  open(context, const TestWillPopScopeWidget());
                }),
            ItemButton(
                value: "Text Spannable",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return TestSpannableWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "Riverpod 状态管理",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return TestRiverpodWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "EasyRefresh ",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return TestRefreshAndLoadMoreWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "pull_to_refresh ",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const PullToRefreshPage();
                    }),
                  );
                }),
            ItemButton(
                value: "RxDart 响应式开发",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestRxDartWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "定位功能的测试",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      Fluttertoast.showToast(msg: "定位功能的测试");
                      // 在这里初始化一个 网络请求
                      return const TestLocationWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "通知栏的开发",
                callback: () {
                  Fluttertoast.showToast(msg: "通知栏的开发 未实现");
                }),
            ItemButton(
                value: "荣大 开发 网络测试",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      Fluttertoast.showToast(msg: "点击打开了网络库的学习");
                      // 在这里初始化一个 网络请求
                      return RDNetInheritedWidget(
                        // api: RDApi("http://192.168.7.188/", "230", true),
                        api: RDApi("https://ibsaas-test-app.rongdasoft.com/", "230", true),
                        child: LoginByPasswordWidget(),
                      );
                    }),
                  );
                }),
            ItemButton(
                value: "基础 数据库 SQL",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      Fluttertoast.showToast(msg: "点击打开了数据库的学习");
                      return SQLPageWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 网络请求 Dio",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestNetDioWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 可滚动组件 PageView ",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestPageViewWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 可滚动组件 ListView ",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestListViewWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 容器类组件 Scaffold 页面骨架",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestScaffoldWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 容器类组件 FittedBox 空间适配",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestFittedBoxWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 容器类组件 Clip 裁切组件",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestClipWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 容器类组件 Container",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestContainerWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 容器类组件 变换Transform",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestTransformWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 容器类组件 DecoratedBox装饰容器类似Shape",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestDecoratedBoxWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 容器类组件 添加Padding的组件",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestPaddingWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 布局组件 对齐与相对定位 Align",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestAlignWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 布局组件 层叠布局 Stack/Positioned",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestStackWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 布局组件 弹性布局 Flex",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestFlexWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 布局组件 流式布局 Wrap Flow",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestWrapWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "Lifecycle",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const LifecycleWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "路由跳转，使用指定预设好的 url -> HomePager",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const MyHomePage(title: "我的主页");
                    }),
                  );
                }),
            ItemButton(
                value: "路由跳转，使用创建Widght -> HomePager",
                callback: () {
                  Navigator.pushNamed(context, "/");
                }),
            ItemButton(
                value: "Android MethodChannel 通讯",
                callback: () {
                  testMethodCall();
                }),
            ItemButton(
                value: "Android BasicMessageChannel 通讯",
                callback: () {
                  initBasicMethodCall();
                }),
            ItemButton(
                value: "Android EventChannel 通讯",
                callback: () {
                  print("Android EventChannel 通讯");
                  initEventChannel();
                }),
            ItemButton(
                value: "基础组件 Text",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestTextWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础组件 Button",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestButtonWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础组件 Image",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestImageWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础组件 Icon",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestIconWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础组件 单选开关和复选框",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestSwitchAndCheckBoxWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础组件 输入框",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestTextFieldWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础组件 表单",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestFormWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础组件 进度指示器",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestProgressWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 布局组件 SizedBox ConstrainedBox",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestLayoutWidget();
                    }),
                  );
                }),
            ItemButton(
                value: "基础 布局组件 线性布局 Row",
                callback: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return const TestRowWidget();
                    }),
                  );
                }),
          ],
        ),
      ),
    );
  }
}

class ItemButton extends StatelessWidget {
  final String value;

  final VoidCallback callback;

  const ItemButton({required this.value, required this.callback, super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Text(value),
        TextButton(
            onPressed: () {
              callback.call();
            },
            child: const Text("Run"))
      ],
    );
  }
}

///和Android进行通讯
String Method_Channel = "com.example.flutter_test_call/methodChannel";
MethodChannel methodChannel = MethodChannel(Method_Channel);

Future<void> testMethodCall() async {
  //1.创建Flutter端的MethodChannel
  //2.通过invokeMethod调用Native方法，拿到返回值
  String debugString = await methodChannel
      .invokeMethod("testMethodCall", ["give me debug info", "give me debug info2"]);
  print('test debugString=$debugString');

  initMethod();
}

void initEventChannel() {
  var eventChannel = const EventChannel("com.example.flutter_test_call/methodChannel");
  var streamSubscription = eventChannel.receiveBroadcastStream().listen((event) {
    print("listen $event");
  }, onError: (dynamic error) {
    print("onError $error");
  }, cancelOnError: true);
}

void initMethod() {
  //1.创建android端的MethodChannel
  methodChannel.setMethodCallHandler((methodCall) async {
    if (methodCall.method == 'nativeCallFlutterMethod') {
      String args = 'nativeCallFlutterMethod 方法被调用了，参数为${methodCall.arguments}';
      // Fluttertoast.showToast(msg: '$args flutterResult');
      return '$args flutterResult';
    }
  });
}

initBasicMethodCall() {
  //1.创建Flutter端的BasicMessageChannel
  BasicMessageChannel basicMessageChannel =
      BasicMessageChannel(Method_Channel, const StringCodec());
  //2.接收来自Native的消息，并向Native回复
  basicMessageChannel.setMessageHandler((message) {
    Logcat.E('test Android端发来的消息 1：$message');
    return Future.value('黄河黄河，我是长江');
  });
  // 进行发送消息
  var send = basicMessageChannel.send("lutter发送到native");
  send.then((value) {
    Logcat.E('test Android端发来的消息 2：$value');
  });
}
